//
//  GYBuyDetailListView.m
//  GYDemo
//
//  Created by Mrzhang on 2018/4/8.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYBuyDetailListView.h"
#import "GYChooseView.h"
#import "Head.h"
#import "GYMySaleProductViewModel.h"
@interface GYBuyDetailListView ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger currentPage;
}
@property(nonatomic,strong)GYChooseView* chooseView;
@property(nonatomic,strong)NSMutableDictionary* paraDataSource;
@property (nonatomic,strong)GYMySaleProductViewModel *likeProductInfoviewModel;
@end

@implementation GYBuyDetailListView
-(GYMySaleProductViewModel *)likeProductInfoviewModel
{
    if (!_likeProductInfoviewModel) {
        _likeProductInfoviewModel=[[GYMySaleProductViewModel alloc] init];
    }
    return _likeProductInfoviewModel;
}
-(NSMutableDictionary *)paraDataSource{
    
    if (!_paraDataSource) {
        _paraDataSource=[[NSMutableDictionary alloc] init];
    }
    return _paraDataSource;
}
-(GYChooseView *)chooseView
{
    if (!_chooseView) {
        _chooseView=[[GYChooseView alloc] initWithFrame:WFCGRectMake(0, 0, 375, 45) chooseViewType:3];
        _chooseView.backgroundColor=[UIColor whiteColor];
        
        [WFFunctions WFUIaddBorderToView:_chooseView top:NO left:0 bottom:1 right:0 borderColor:RGBACOLOR(240, 240, 240, 1) borderWidth:0.5];
        @WeakSelf;
        _chooseView.chooseBack = ^(NSDictionary *paraDic) {
            for (NSString* key in paraDic.allKeys)
            {
                [selfp.paraDataSource setObject:paraDic[key] forKey:key];
            }
            currentPage=1;
            [selfp.paraDataSource setObject:@"1" forKey:@"page"];
           // [selfp reloadData];
        };
        _chooseView.categoryback = ^(NSString *categoryStr) {
            currentPage=1;
            [selfp.paraDataSource setObject:@"1" forKey:@"page"];
            [selfp.paraDataSource setObject:categoryStr forKey:@"sort"];
            //[selfp reloadData];
            
        };
    }
    return _chooseView;
}

-(UITableView *)myTable
{
    if (!_myTable) {
        _myTable=[[GCBaseTableView alloc]initWithFrame:self.bounds style:UITableViewStyleGrouped];
        _myTable.delegate=self;
        _myTable.dataSource=self;
        _myTable.separatorStyle=UITableViewCellSeparatorStyleNone;
        _myTable.backgroundColor=[UIColor whiteColor];
        
        _myTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            currentPage = 1;
            [self setOriginData:self.paraDataSource[@"protypeid"]];
     
        }];
        _myTable.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
            if (self.likeProductInfoviewModel.canAddMore)
            {
                currentPage += 1;
                [self.paraDataSource setObject:[NSString stringWithFormat:@"%ld",currentPage] forKey:@"page"];
                [self.likeProductInfoviewModel requestDatas:self.paraDataSource];

            }else
            {
                [_myTable.mj_footer endRefreshingWithNoMoreData];
            }
            
        }];
    }
    return _myTable;
}
-(void)setOriginData:(NSString* )proID
{
    currentPage = 1;
//    for (NSString* key in self.paraDataSource.allKeys) {
//        if (![key isEqualToString:@"sort"])
//        {
//            [self.paraDataSource removeObjectForKey:key];
//        }
//    }
//    if (![WFFunctions WFStrCheckEmpty:proID]) {
//        self.paraDataSource[@"protypeid"]=[NSString stringWithFormat:@"%@",proID];
//    }
    self.paraDataSource[@"protypeid"]=[NSString stringWithFormat:@"%@",proID];
    self.paraDataSource[@"page"]=@"1";
    self.paraDataSource[@"pagesize"]=@"10";
    self.paraDataSource[@"state"]=@"1";
    
    [self.likeProductInfoviewModel requestDatas:self.paraDataSource];
    
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self)
    {
     
        [self.likeProductInfoviewModel addObserver:self forKeyPath:@"datas" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
        [self.likeProductInfoviewModel addObserver:self forKeyPath:@"totalCount" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
        currentPage=1;
        [self addSubview:self.myTable];
    }
    return self;
}
#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.likeProductInfoviewModel.datas.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellID = @"CellID";
    GYMineLikeProductCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[GYMineLikeProductCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    GYMineLikeProductInfoModel *model = self.likeProductInfoviewModel.datas[indexPath.row];
    cell.model = model;
    [cell.deleteBtn setTitle:@"确定供货" forState:UIControlStateNormal];
    [cell.deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cell.deleteBtn.backgroundColor=Main_Color_Blue;
    [WFFunctions WFUIaddbordertoView:cell.deleteBtn radius:4 width:0.7f color:[UIColor clearColor]];
    @WeakSelf;
    [cell.deleteBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        [selfp reqeustCheckSuplly:model.id];
    }];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = WFCGFloatY(104);
    return height;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return .0001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
#pragma mark - life
-(void)dealloc
{
    [[GYUserViewModel shareInterface] removeObserver:self forKeyPath:@"datas"];
    [[GYUserViewModel shareInterface] removeObserver:self forKeyPath:@"totalCount"];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"datas"]) {
        [self.myTable reloadData];
      //  [self setCompanyBaseModel:[GYUserViewModel shareInterface].companyBaseModel];
    }
    if ([keyPath isEqualToString:@"totalCount"])
    {
         [self.chooseView setTotalCount:self.likeProductInfoviewModel.totalCount];
    }
}
-(void)reqeustCheckSuplly:(NSString* )saleid
{
    @WeakSelf;
    NSString *interface = @"supplyCheck";
    NSMutableDictionary* para=[[NSMutableDictionary alloc] init];
    [para setObject:self.paraDataSource[@"protypeid"] forKey:@"protypeid"];
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        [selfp requestDoSupply:saleid];

    }];
}
-(void)requestDoSupply:(NSString* )saleid
{
    NSString *interface = @"sureSupply";
    NSMutableDictionary* para=[[NSMutableDictionary alloc] init];
    [para setObject:_PassID forKey:@"buyproid"];
    [para setObject:saleid forKey:@"saleproid"];
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        [FTIndicator showSuccessWithMessage:@"供货成功"];
        
    }];
}
@end
